Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

valtio

Package Overview
Dependencies
Maintainers
2
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

valtio

💊 Valtio makes proxy-state simple for React and Vanilla

  • 1.13.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is valtio?

Valtio is a proxy-state library for React that makes it easy to manage and mutate state with a simple and intuitive API. It leverages JavaScript proxies to create reactive state objects that automatically trigger re-renders in your React components when the state changes.

What are valtio's main functionalities?

Reactive State Management

Valtio allows you to create reactive state objects using the `proxy` function. The `useSnapshot` hook is used to subscribe to state changes and trigger re-renders in your React components.

const { proxy, useSnapshot } = require('valtio');

const state = proxy({ count: 0 });

function Counter() {
  const snap = useSnapshot(state);
  return (
    <div>
      <p>{snap.count}</p>
      <button onClick={() => state.count++}>Increment</button>
    </div>
  );
}

Derived State

Valtio supports derived state using the `derive` function. This allows you to create computed properties that automatically update when the underlying state changes.

const { proxy, useSnapshot, derive } = require('valtio');

const state = proxy({ count: 0 });

derive({
  doubleCount: (get) => get(state).count * 2
});

function Counter() {
  const snap = useSnapshot(state);
  return (
    <div>
      <p>{snap.count}</p>
      <p>{snap.doubleCount}</p>
      <button onClick={() => state.count++}>Increment</button>
    </div>
  );
}

Middleware

Valtio provides a `subscribe` function to listen for state changes and execute side effects. This is useful for logging, analytics, or other middleware-like functionalities.

const { proxy, subscribe } = require('valtio');

const state = proxy({ count: 0 });

subscribe(state, () => {
  console.log('State changed:', state.count);
});

state.count++;

Other packages similar to valtio

Keywords

FAQs

Package last updated on 02 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc